// callDoubao.ts
import axios from "axios";
import { addAssistantMessage, addUserMessage, getSessionMessages } from "./messageStore";

export async function callDoubaoIntent(text: string, sessionId: string): Promise<string> {
  addUserMessage(sessionId, text);

  const messages = getSessionMessages(sessionId);
  const model = "doubao-seed-1-6-thinking-250715";
  console.log(
    `📤 豆包messages数组: ${JSON.stringify(messages, null, 2)}`
  );
  const response = await axios.post(
    "https://ark.cn-beijing.volces.com/api/v3/chat/completions",
    { model, messages },
    {
      headers: {
        Authorization: `Bearer ${process.env.DOUBAO_API_KEY}`,
        "Content-Type": "application/json",
      },
    }
  );
  console.log(
    `📤 豆包返回内容数组: ${JSON.stringify(response.data?.choices?.[0], null, 2)}`
  );
  const content = response.data?.choices?.[0]?.message?.content?.trim() ?? "";
  addAssistantMessage(sessionId, content);
  return content;
}
